<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-card shadow="hover">
      <el-form
        ref="extraForm"
        :model="extraForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="地产ID" prop="estateId">
          <el-input
            v-model.trim="extraForm.estateId"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="样图">
          <el-upload
            ref="imageUrl"
            :file-list="imageUrlfileList"
            action="imageUrl"
            list-type="picture-card"
            :before-upload="imageUrlBeforeUpload"
            :on-remove="removeFile"
            name="file"
            :http-request="uploadImage"
            multiple
            :limit="6"
          >
            <i class="el-icon-plus"></i>
            <!-- <el-button size="small" type="primary" icon="el-icon-upload">
              点击上传
            </el-button> -->
          </el-upload>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最低价格(万元)" prop="priceMin">
              <el-input-number
                v-model.trim="extraForm.priceMin"
                autocomplete="off"
                :min="0"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最高价格(万元)" prop="priceMax">
              <el-input-number
                v-model.trim="extraForm.priceMax"
                autocomplete="off"
                :min="0"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="地产标签" prop="tags">
          <el-select
            v-model="extraForm.tags"
            collapse-tags
            multiple
            placeholder="请选择"
            size="medium"
            clearable
          >
            <el-option
              v-for="item in buildingTags"
              :key="item.constantId"
              :label="item.constantValue"
              :value="item.constantId + ''"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目描述">
          <el-input
            v-model.trim="extraForm.attributes.desc"
            type="textarea"
            placeholder="请输入项目描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="咨询方式">
          <el-input
            v-model.trim="extraForm.attributes.phone"
            type="number"
            placeholder="请输入咨询方式"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { setExtraInfo, getExtraInfo, updateExtraInfo } from '@/api/building'
  import { getByType, getHouseType } from '@/api/tag'
  import { upload } from '@/api/upload'
  import _net from '../../../config/net.config'
  import _ from 'lodash'
  export default {
    name: 'ExtraInfo',
    data() {
      return {
        buildingTags: [],
        imageUrlfileList: [],
        extraForm: {
          estateId: '',
          imgs: [],
          priceMax: 0,
          priceMin: 0,
          tags: [],
          attributes: { desc: '', phone: '' },
        },
        buildingVo: [],
        rules: {
          userName: [
            { required: true, trigger: 'blur', message: '请输入用户昵称' },
          ],
          priceMin: [
            { required: true, trigger: 'blur', message: '请输入最低价格' },
          ],
          priceMax: [
            { required: true, trigger: 'blur', message: '请输入最高价格' },
          ],
          tags: [
            { required: true, trigger: 'blur', message: '请选择地产标签' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        show: false,
        isEdit: false,
      }
    },
    methods: {
      imageUrlBeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
      removeFile(file, fileList) {
        console.log(file)
        console.log(fileList)
        let _temp = this.imageUrlfileList.filter(function (item) {
          return item.id != file.id
        })
        this.imageUrlfileList = _temp
      },
      async uploadImage(param) {
        console.log(param)
        const formData = new FormData()
        formData.append('fileType', 1)
        formData.append('file', param.file)
        console.log(formData)
        const { data } = await upload(formData)
        if (data.status == 0) {
          // let base = 'https://app.aixinfang.vip'
          let base = _net.baseURL
          const element = base + '/file/download?fileId=' + data.body.fileId
          this.imageUrlfileList.push({ url: element, id: data.body.fileId })
        } else {
          this.$baseMessage(data.message, 'error')
        }
        // this.extraForm.imgs.push(data.body.fileId)
        // if (data.status == 0) {
        //   this.push
        // } else {
        // }
        console.log(this.imageUrlfileList)
      },

      async showEdit(row) {
        if (row.floorId) {
          this.title = '配置额外信息'
          const { data } = await getExtraInfo(row.floorId)
          if (data.status == 0) {
            console.log(data.body)
            if (data.body != null) {
              this.isEdit = true
              let clone = _.cloneDeep(data.body)
              let _imagIds = clone.imgs == null ? [] : clone.imgs.split(',')
              let _file = []
              // let base = 'https://app.aixinfang.vip'
              let base = _net.baseURL
              for (let index = 0; index < _imagIds.length; index++) {
                const element =
                  base + '/file/download?fileId=' + _imagIds[index]
                _file.push({ url: element, id: _imagIds[index] })
              }
              console.log(_file)
              this.imageUrlfileList = _file
              clone.tags = clone.tags.split(',')
              this.extraForm = clone
            } else {
              this.isEdit = false
              this.imageUrlfileList = []
              this.resize()
            }
          } else {
            this.$baseMessage(data.message, 'error')
          }

          this.show = true
          this.buildingVo = Object.assign({}, row)
          this.extraForm.estateId = row.floorId
          this.fetchBuildingTagData()
        }
        this.dialogFormVisible = true
      },
      resize() {
        this.extraForm = {
          estateId: '',
          imgs: [],
          priceMax: 0,
          priceMin: 0,
          tags: [],
          attributes: { desc: '', phone: '' },
        }
      },
      close() {
        console.log('准备关闭')
        this.dialogFormVisible = false
        this.resize()
      },
      // 保存额外信息
      save() {
        this.$refs['extraForm'].validate(async (valid) => {
          if (valid) {
            let item = _.cloneDeep(this.extraForm)
            if (this.extraForm.tags.length == 0) {
              this.$baseMessage('请选择地产标签', 'error')
              return
            } else if (this.extraForm.priceMax == 0) {
              this.$baseMessage('最高价不能为0', 'error')
              return
            } else if (this.extraForm.priceMin == 0) {
              this.$baseMessage('最低价不能为0', 'error')
              return
            } else if (this.extraForm.attributes.phone == '') {
              this.$baseMessage('咨询方式不能为空', 'error')
              return
            } else if (this.imageUrlfileList.length == 0) {
              this.$baseMessage('上传的图片不能为空', 'error')
              return
            }
            let temp = []
            for (let index = 0; index < this.imageUrlfileList.length; index++) {
              const element = this.imageUrlfileList[index].id
              temp.push(element)
            }
            item.tags = this.extraForm.tags.toString()
            item.imgs = temp.toString()
            item.priceMax = Number(item.priceMax)
            item.priceMin = Number(item.priceMin)
            if (this.isEdit) {
              const { data } = await updateExtraInfo(item)
              if (data.status == 0) {
                this.$baseMessage('更新成功!', 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(data.message, 'success')
              }
            } else {
              const { data } = await setExtraInfo(item)
              if (data.status == 0) {
                this.$baseMessage('设置成功！', 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(data.message, 'success')
              }
            }
          }
        })
      },
      async fetchBuildingTagData() {
        let pushData = {}
        pushData.type = 4
        const { data } = await getByType(pushData)
        this.buildingTags = data.body
        this.$baseNotify('地产标签获取完毕！', '成功', 'success', 'top-right')
      },
    },
  }
</script>
<style lang="scss" scoped>
  .vab-upload {
    z-index: 9999 !important;
  }
</style>
